package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishSetmealMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishSetmealMapper dishSetmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 新增套餐和保存套餐和菜品的关联关系
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savewithDishs(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //复制对应属性
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入一条套餐数据
        setmealMapper.insert(setmeal);

        //获取插入套餐数据后的套餐id
        Long id = setmeal.getId();
        //插入n条套餐菜品数据
        //如果套餐菜品数据不为空，则插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null&&setmealDishes.size()>0){
            //为套餐菜品数据赋值上套餐id
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            });
            dishSetmealMapper.inserBatch(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //利用pagehelper进行分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //获取page对象
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);

        //封装数据
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total,result);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    //删除套餐的同时也要删除套餐和菜品的关联关系，要操作两个表，所以要开启事务管理
    public void deleteBatch(List<Long> ids) {
        //先判断套餐状态是否起售中，如果是起售中则不能删除
        for(Long id:ids){
           Setmeal setmeal= setmealMapper.selectById(id);
           if(setmeal.getStatus().equals(StatusConstant.ENABLE)){
               //套餐在起售中，不能删除
               throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
           }
        }
        for (Long id : ids) {
            //先删除id对应的套餐
            setmealMapper.deleteById(id);
            //再删除套餐id对应的套餐菜品关联关系
            dishSetmealMapper.deleteBySetmealId(id);
        }
    }

    /**
     * 通过id修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetmealWithSetmealDishs(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //先修改套餐表中的信息
        setmealMapper.update(setmeal);

        //获取套餐id用于修改套餐菜品关系表
        Long setmealId = setmeal.getId();

        //先删除套餐id对应的套餐菜品关联关系
        dishSetmealMapper.deleteBySetmealId(setmealId);

        //插入n条套餐菜品数据
        //如果套餐菜品数据不为空，则插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null&&setmealDishes.size()>0){
            //为套餐菜品数据赋值上套餐id
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            dishSetmealMapper.inserBatch(setmealDishes);
        }
    }

    /**
     * 通过id查询套餐和其下属的菜品
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //先查询对应的套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);

        //再通过套餐id查询下属的菜品套餐关系
        List<SetmealDish> setmealDishList = dishSetmealMapper.selectBySetmealId(id);

        //封装返回数据
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 起售或停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //如果为起售操作，要先判断下套餐中的菜品是否有停售的，如果有则无法起售
        if(status==StatusConstant.ENABLE){
            //这句是关键，通过setmealId直接获得菜品信息，而不是通过套餐菜品关系表先获得菜品id再获得菜品信息
            List<Dish>dishList= dishMapper.getBySetmealId(id);
            if(dishList!=null&&dishList.size()>0){
                for (Dish dish : dishList) {
                    if(dish.getStatus().equals(StatusConstant.DISABLE))
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
